package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.dao.CheckItemDao;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.CheckItem;
import com.xrit.sunnyday.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
@Transactional
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    /**
     * 查询所有检查项(不分页)
     * */
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
    /**
     * 添加检查项
     * */
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }
    /**
     * 查询所有检查项并判断用户是否使用搜索框查询（分页）
     * */
    @Override
    public PageInfo<CheckItem> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        List<CheckItem> list = checkItemDao.findPage(queryPageBean.getQueryString());
        return new PageInfo<>(list);
    }
    /**
     * 根据ID删除检查项
     * */
    @Override
    public void deleteById(Integer id) {
        int count = checkItemDao.findCountById(id);
        if(count>0){
            throw new SunnydayException("该检查项已经被检查组关联");
        }

        checkItemDao.deleteById(id);
    }
    /**
     * 编辑：根据ID查询某项
     * */
    @Override
    public CheckItem selectById(Integer id) {
        return checkItemDao.selectById(id);
    }
    /**
     * 编辑：根据ID修改
     * */
    @Override
    public void updateById(CheckItem checkItem) {
        checkItemDao.updateById(checkItem);
    }
}
